package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.ReportDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.pojo.vo.MemberReportVO;
import com.itheima.health.pojo.vo.SetmealReport;
import com.itheima.health.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private ReportDao reportDao;

    /**
     * 统计分析-套餐预约占比饼型图
     * 统计分析-会员数量折线图
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        //获得当前日期
        LocalDate currentDate = LocalDate.now();
        String today = currentDate.format(formatter);
        //获得本周一的日期
        LocalDate today02 = LocalDate.now();
        LocalDate mondayOfThisWeek = today02.with(DayOfWeek.MONDAY);
        String thisWeekMonday = mondayOfThisWeek.format(formatter);
        //获得本月第一天的日期
        LocalDate today03 = LocalDate.now();
        LocalDate firstDayOfMonth = today03.with(TemporalAdjusters.firstDayOfMonth());
        String firstDay4ThisMonth = firstDayOfMonth.format(formatter);

        //今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);

        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();

        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);

        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);

        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);

        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);

        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);

        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);

        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐（取前4）
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", today);
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);

        return result;
    }

    public SetmealReport getSetmealReport() {

        // 查询套餐名
        List<String> setmealName = setMealDao.selectSetmealName();
        // 查询套餐名以及数量
        List<Map<String, String>> setmealCount = setMealDao.selectSetmealNameAndCount();
        // 构造对象
        return SetmealReport.builder()
                .setmealNames(setmealName)
                .setmealCount(setmealCount)
                .build();
    }


    public MemberReportVO getMemberReport() {

        //创建集合用来存储每个月的LocalDate对象和Integer数量对象
        List<String> monthList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();

        //获取当前日期
        LocalDate now = LocalDate.now();
        //一年前的开始月份
        LocalDate monthBegin = now.minusMonths(13).with(TemporalAdjusters.firstDayOfMonth());
        //开始月份的最后一天
        LocalDate monthEnd = monthBegin.plusMonths(2)
                .with(TemporalAdjusters.firstDayOfMonth())
                .minusDays(1);
        for (int i = 0; i < 12; i++) {
            //获取该月份最后一天前的里面的所有会员数量
            Integer count = reportDao.getMemberCount(monthEnd);
//            String year = String.valueOf(monthEnd.getYear());
//            String month = String.valueOf(monthEnd.getMonthValue();
            String yearAndMonth =(monthEnd.getYear()) + "-" + monthEnd.getMonthValue();
            monthList.add(yearAndMonth);
            countList.add(count);
            //向后推一个月
            monthEnd = monthEnd.plusMonths(2)
                    .with(TemporalAdjusters.firstDayOfMonth())
                    .minusDays(1);
        }
        return MemberReportVO.builder()
                .months(monthList)
                .memberCount(countList)
                .build();
    }
}
